package cn.xiaou.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public final class ImageManagerUtils {
	
	private ImageManagerUtils() {};
	/**
	 * 获得ico图片路径
	 */
	public static final String icoUrl=ImageManagerUtils.getRoot()+"\\img\\ico.png";
	public static final String loginView=ImageManagerUtils.getRoot()+"\\img\\loginView.png";
	public static final String MainFrameBg=ImageManagerUtils.getRoot()+"\\img\\MainFrameBg.png";
	public static final String AboutIco=ImageManagerUtils.getRoot()+"\\img\\AboutIco.png";
	public static final String informationBg=ImageManagerUtils.getRoot()+"\\img\\informationBg.jpg";
	public static final String AboutTheWings=ImageManagerUtils.getRoot()+"\\img\\AboutTheWings.png";
	public static final String RealNameBg=ImageManagerUtils.getRoot()+"\\img\\RealNameBg.png";
	public static final String Confirm=ImageManagerUtils.getRoot()+"\\img\\Confirm.png";
	public static final String Close=ImageManagerUtils.getRoot()+"\\img\\Close.png";
	public static final String InformationBg=ImageManagerUtils.getRoot()+"\\img\\InformationBg.png";
	
	public static final String b1=ImageManagerUtils.getRoot()+"\\img\\b1.png";
	public static final String b2=ImageManagerUtils.getRoot()+"\\img\\b2.png";
	public static final String b3=ImageManagerUtils.getRoot()+"\\img\\b3.png";
	public static final String b4=ImageManagerUtils.getRoot()+"\\img\\b4.png";
	public static final String b5=ImageManagerUtils.getRoot()+"\\img\\b5.png";
	
	public static final String Sdown=ImageManagerUtils.getRoot()+"\\img\\down.png";
	public static final String Sup=ImageManagerUtils.getRoot()+"\\img\\up1.png";
	
	public static final String rdown=ImageManagerUtils.getRoot()+"\\img\\rdown.png";
	public static final String rup=ImageManagerUtils.getRoot()+"\\img\\rup.png";
	public static final String rdown2=ImageManagerUtils.getRoot()+"\\img\\rdown2.png";
	public static final String reBG=ImageManagerUtils.getRoot()+"\\img\\reBg.png";
	
	public static final String submitBg=ImageManagerUtils.getRoot()+"\\img\\submitBg.png";
	public static final String resetBg=ImageManagerUtils.getRoot()+"\\img\\resetBg.png";
	public static final String backBg=ImageManagerUtils.getRoot()+"\\img\\backBg.png";
	
	public static final String ProgressBarBg=ImageManagerUtils.getRoot()+"\\img\\ProgressBarBg.png";
	
	public static final String aboutUsBg=ImageManagerUtils.getRoot()+"\\img\\aboutUsBg.png";
	public static final String logoutBg=ImageManagerUtils.getRoot()+"\\img\\logoutBg.png";
	public static final String logoutWhiteBg=ImageManagerUtils.getRoot()+"\\img\\logoutWhiteBg.png";
	public static final String personalHoldingsBg=ImageManagerUtils.getRoot()+"\\img\\personalHoldingsBg.png";
	public static final String realNameICO=ImageManagerUtils.getRoot()+"\\img\\realNameICO.png";
	public static final String replenishBg=ImageManagerUtils.getRoot()+"\\img\\replenishBg.png";
	public static final String stockInfoBg=ImageManagerUtils.getRoot()+"\\img\\stockInfoBg.png";
	public static final String userInfoBg=ImageManagerUtils.getRoot()+"\\img\\userInfoBg.png";

	public static final String exit=ImageManagerUtils.getRoot()+"\\img\\exit.png";
	public static final String sellBg=ImageManagerUtils.getRoot()+"\\img\\sellBg.png";
	
	public static final String black=ImageManagerUtils.getRoot()+"\\img\\black.jpg";
	
	public static final String qdd=ImageManagerUtils.getRoot()+"\\img\\qdd.png";
	public static final String maichu=ImageManagerUtils.getRoot()+"\\img\\maichu.png";
	/**
	 * 获得工程的目录的绝对路径
	 * 时间2018-06-19 12:45:55
	 */
	public final static String getRoot() {
		try {
			return new File("").getCanonicalPath()+"\\";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return"";
	}
	/**
	 * 通过文件路径获得二进制数据
	 * @param file 
	 * @param fileSize 文件大小限制，单位字节
	 * @return 成功返回byte数组,失败返回null（包括文件超过大小）
	 */
	public static  byte[] getImageByte(File file,int fileSize)  {
		FileInputStream inputStream = null;
		ByteArrayOutputStream out = null;
		try {
			inputStream = new FileInputStream(file);
			int imageSize = inputStream.available();
			if(imageSize > fileSize) {
				return null;
			}
			out = new ByteArrayOutputStream(imageSize);
			int len =0;
			byte[] temp;
			if(imageSize<1024)  temp =new byte[imageSize]; else temp =new byte[1024];
			while((len = inputStream.read(temp,0,temp.length))!=-1) {
				out.write(temp,0,len);
			}
		}catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out!=null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return out.toByteArray();
	}
	/**
	 * 通过文件路径获得二进制数据
	 * @param file 
	 * @return 成功返回byte数组,失败返回null
	 */
	public static  byte[] getImageByte(File file)  {
		FileInputStream inputStream = null;
		ByteArrayOutputStream out = null;
		try {
			 inputStream = new FileInputStream(file);
			int imageSize = inputStream.available();
			 out = new ByteArrayOutputStream(imageSize);
			int len =0;
			byte[] temp;
			if(imageSize<1024)  temp =new byte[imageSize]; else temp =new byte[1024];
			while((len = inputStream.read(temp,0,temp.length))!=-1) {
				out.write(temp,0,len);
			}
		}catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out!=null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return out.toByteArray();
	}
	/**
	 * 
	 * @param sql sql 语句查询数据库的二进制文件
	 * @param path 要保存的路径如D:/1.jpg
	 * @param field 数据库二进制字段
	 * @return true成功，false失败
	 */
	public static Boolean downloadImage(String sql,String path,String field)  {
		Connection conn;
		Statement st = null;
		ResultSet query;
		InputStream in = null;
		 FileOutputStream out = null;
		try {
			 conn = DataSourceUtils.getConnection();
			 st = conn.createStatement();
			 query = st.executeQuery(sql);
			 while(query.next()) {
				 in = query.getBinaryStream(field);
				 out = new FileOutputStream(new File(path));
				 byte[] buffer = new byte[1024];
	             int len = 0;
	             while ((len = in.read(buffer)) != -1) {
	                 out.write(buffer, 0, len);
	             }
			 }
		}catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}finally {
			try {
				DataSourceUtils.closeConnection();
				if(st!=null) {
					st.close();
				}
				if(in != null) {
					in.close();
				}
				if(out!=null) {
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	
}
